/*
 * @Autor: 黄旗
 * @Date: 2020-06-02 17:47:27
 */
// Vue
import Vue from 'vue';
import App from './App';
import Setting from './setting';

// 配置
// import Setting from './setting';

// 混合
import mixinApp from '@/mixins/app';

// 插件
import plugins from '@/plugins';

// store
import store from '@/store/index';

import * as $websocket from './plugins/request/websocket';

// iView 和 iView Pro
import iView from 'view-design';
import iViewPro from '@/libs/iview-pro/iview-pro.min.js';

// 菜单和路由
import router from './router';
// import menuHeader from '@/menu/header';
// import menuSider from '@/menu/sider';
import { frameInRoutes } from '@/router/routes';

// 多语言
import i18n from '@/i18n';

// 方法
import { getHeaderName, getMenuSider, getSiderSubmenu } from '@/libs/system';

// 内置组件
import iLink from '@/components/link';
// css样式
import './styles/qqface.less';

// 使用样式，修改主题可以在 styles 目录下创建新的主题包并修改 iView 默认的 less 变量
// 参考 https://www.iviewui.com/docs/guide/theme
import './styles/index.less';
import './libs/iview-pro/iview-pro.css';
import VueLazyload from 'vue-lazyload'; // 引入这个懒加载插件
// 图片预览插件
import Viewer from 'v-viewer';
import 'viewerjs/dist/viewer.css';
// 局部打印
import iPrint from '@/components/print/print.min';
Vue.prototype.$websocket = $websocket;
Vue.use(VueLazyload, { // 添加自定义选项
    preLoad: 1.3,
    error: './assets/default.png', // 加载错误时候的图片
    loading: './assets/default.png', // 加载中的图片
    attempt: 7,
    listenEvents: ['scroll']
});
Vue.prototype.$Setting = Setting;
Vue.use(iPrint);

Viewer.setDefaults({
    url: "data-source",
    filter(image) {
      let dataSource = image.getAttribute('data-source')
      if (dataSource == null || dataSource == undefined) {
        return false;
      } else {
        return true;
      }
    },
    Options: {
        inline: true,
        button: true,
        navbar: true,
        title: true,
        toolbar: true,
        tooltip: true,
        movable: true,
        zoomable: true,
        rotatable: true,
        scalable: true,
        transition: true,
        fullscreen: true,
        keyboard: true,
        url: 'data-source'
    }
});
Vue.use(Viewer);
if (window) window.$t = (key, value) => i18n.t(key, value);

Vue.use(plugins);

Vue.use(iView, {
    i18n: (key, value) => i18n.t(key, value)
});
Vue.use(iViewPro);
Vue.component('i-link', iLink);

new Vue({
    mixins: [mixinApp],
    router,
    store,
    i18n,
    render: (h) => h(App),
    created () {
        // 处理路由 得到每一级的路由设置
        this.$store.commit('admin/page/init', frameInRoutes);
        // 设置顶栏菜单
        // this.$store.commit('admin/menu/setHeader', menuHeader);
        // 加载用户登录的数据
        this.$store.dispatch('admin/account/load');
        // 初始化全屏监听
        this.$store.dispatch('admin/layout/listenFullscreen');
    },
    watch: {
        // 监听路由 控制侧边栏显示 标记当前顶栏菜单（如需要）
        $route (to, from) {
            if (
                localStorage.getItem('menus') &&
                localStorage.getItem('menus') !== '{}'
            ) {
                const path = to.path;
                let menus;
                menus = JSON.parse(localStorage.getItem('menus'));
                const headerName = getHeaderName(path, menus.sider);
                // if (headerName !== null) {
                this.$store.commit('admin/menu/setHeaderName', headerName);
                const filterMenuSider = getMenuSider(menus.sider, headerName);
                this.$store.commit('admin/menu/setSider', filterMenuSider);
                this.$store.commit('admin/menu/setActivePath', path);
                const openNames = getSiderSubmenu(path, menus.sider);
                this.$store.commit('admin/menu/setOpenNames', openNames);
                // }
                this.$store.commit('admin/menu/setActivePath', path);
                this.appRouteChange(to, from);
            }
        }
    }
}).$mount('#app');
